//
//  OutCollectView.m
//  CMOLTest
//
//  Created by glendale on 16/3/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CheckCollectView.h"
#import "CheckCollectionCell.h"

#import "UIView+Extension.h"
#import "SDPhotoBrowser.h"
@interface CheckCollectView()<SDPhotoBrowserDelegate>

@end

@implementation CheckCollectView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCollectionView];
    }
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self creatCollectionView];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout  alloc] init];
    layout.itemSize = CGSizeMake(120, 120);
    layout.minimumInteritemSpacing = 20;
    layout.sectionInset=UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _photoCollect.showsHorizontalScrollIndicator = NO;
    _photoCollect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 130) collectionViewLayout:layout];
    _photoCollect.backgroundColor = [UIColor clearColor];
    _photoCollect.dataSource = self;
    _photoCollect.delegate=self;
    [_photoCollect registerNib:[UINib nibWithNibName:@"CheckCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CheckCollectionCell"];
    [self addSubview:_photoCollect];
}
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
 

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoes.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CheckCollectionCell *outCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CheckCollectionCell" forIndexPath:indexPath];
    outCell.indexpath=indexPath;

    NSString *placeholdImage=[ICrEAMTool isVW]?@"vsw_placehold":@"skd_placehold";
    
    if (self.isEdit) {
        outCell.deleteBtn.hidden=NO;
    }else{
        outCell.deleteBtn.hidden=YES;
    }
    

    id obj= self.photoes[indexPath.row];
    if ([obj isKindOfClass:[UIImage class]]) {
        outCell.CheckimageView.image=obj;
        
    }
    if ([obj isKindOfClass:[NSString class]]) {
        NSURL *url=[NSURL URLWithString:(NSString *)obj];
//          [[SDWebImageDownloader sharedDownloader] setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,image/webq,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
        
        [outCell.CheckimageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:placeholdImage]];
    }
    return outCell;
}
-(void)setPhotoes:(NSArray *)photoes{
    _photoes=[photoes mutableCopy];
    [_photoCollect reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]initWithFrame:[UIScreen mainScreen].bounds];
    browser.delegate=self;
    browser.imageCount=self.photoes.count;
    browser.currentImageIndex=indexPath.row;
    [browser show];
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    NSString *placeholdImage=[ICrEAMTool isVW]?@"vsw_placehold":@"skd_placehold";
    return [UIImage imageNamed:placeholdImage];
}

- (id)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
    id obj=self.photoes[index];
    if ([obj isKindOfClass:[NSString class]]){
         return [NSURL URLWithString:self.photoes[index]];
    }
    return obj;
   
}
@end
